package org.ssg.dataway.config.swagger;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Slf4j
@ConditionalOnBean({UiConfiguration.class})
@EnableSwagger2
@Configuration()
public class SwaggerConfig  implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        log.info("===============createRestApi=================");
        return new Docket(DocumentationType.SWAGGER_2).groupName("dataway项目")
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.ssg.dataway"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("欢迎到我的项目：https://gitee.com/jack115586888/dataway")
                .termsOfServiceUrl("https://gitee.com/jack115586888/dataway")
                .contact("ssg").version("1.0")//
                .build();
    }


    /**
     * 设置静态资源路径的映射
     */
//    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("MyWebMvcConfigurer2 addResourceHandlers ================================");
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}